// AdditionalObj.h : Declaration of the CAdditionalObj

#pragma once
#include "resource.h"       // main symbols



#include "OutlookAddin_i.h"
#include "SyncSetting.h"


#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

using namespace ATL;
using namespace Office;
using namespace Outlook;

// CAdditionalObj

class ATL_NO_VTABLE CAdditionalObj :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CAdditionalObj, &CLSID_AdditionalObj>,
	public IDispatchImpl<IAdditionalObj, &IID_IAdditionalObj, &LIBID_OutlookAddinLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
	public IDispatchImpl<_IDTExtensibility2, &__uuidof(_IDTExtensibility2), &LIBID_AddInDesignerObjects, /* wMajor = */ 1>,
	public IDispatchImpl<IRibbonExtensibility, &__uuidof(IRibbonExtensibility), &__uuidof(__Office), /* wMajor = */ 2, /* wMinor = */ 5>,
	public IDispatchImpl<IRibbonCallback, &__uuidof(IRibbonCallback)>
{
public:
	Outlook::_ApplicationPtr spAppCache;
	CAdditionalObj()
	{
		spSyncSettingForm = NULL;
	}

DECLARE_REGISTRY_RESOURCEID(IDR_ADDITIONALOBJ)


BEGIN_COM_MAP(CAdditionalObj)
	COM_INTERFACE_ENTRY(IAdditionalObj)
	COM_INTERFACE_ENTRY2(IDispatch, _IDTExtensibility2)
	COM_INTERFACE_ENTRY(_IDTExtensibility2)
	COM_INTERFACE_ENTRY(IRibbonExtensibility)
	COM_INTERFACE_ENTRY(IRibbonCallback)
END_COM_MAP()



	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
		delete spSyncSettingForm;
	}

public:
	STDMETHOD(SyncSettingClicked)(IDispatch* ribbon);


	STDMETHOD(Invoke)(DISPID dispidMember, 
			const IID &riid, 
			LCID lcid, 
			WORD wFlags, 
			DISPPARAMS *pdispparams, 
			VARIANT *pvarResult, 
			EXCEPINFO *pexceptinfo, 
			UINT *puArgErr)
	{
		HRESULT hr=DISP_E_MEMBERNOTFOUND;
		if(dispidMember==42)
		{
			hr  = IDispatchImpl<IRibbonCallback, &__uuidof(IRibbonCallback)>::Invoke(dispidMember, 
				riid, 
				lcid, 
				wFlags,       
				pdispparams, 
				pvarResult, 
				pexceptinfo, 
				puArgErr);
		}
		if (DISP_E_MEMBERNOTFOUND == hr)
			hr = IDispatchImpl<_IDTExtensibility2, &__uuidof(_IDTExtensibility2), &LIBID_AddInDesignerObjects, /* wMajor = */ 1>::Invoke(dispidMember, 
				riid, 
				lcid, 
				wFlags, 
				pdispparams, 
				pvarResult, 
				pexceptinfo, 
				puArgErr);
		return hr;
	}

	STDMETHOD(GetIDsOfNames)(
		_In_ REFIID riid, 
		_In_count_(cNames) _Deref_pre_z_ LPOLESTR* rgszNames, 
		_In_ UINT cNames,
		_In_ LCID lcid, 
		_Out_ DISPID* rgdispid)
		{
			HRESULT hr=S_OK;
			if(wcscmp(*rgszNames,L"SyncSettingClicked") == 0)
				*rgdispid = 42;
			return hr;
		}

	// _IDTExtensibility2 Methods
public:
	
	STDMETHOD(OnConnection)(LPDISPATCH Application, ext_ConnectMode ConnectMode, LPDISPATCH AddInInst, SAFEARRAY * * custom);
	STDMETHOD(OnDisconnection)(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom);
	STDMETHOD(OnAddInsUpdate)(SAFEARRAY * * custom);
	STDMETHOD(OnStartupComplete)(SAFEARRAY * * custom);
	STDMETHOD(OnBeginShutdown)(SAFEARRAY * * custom);

	//IRibbonExtensibility
public:
	STDMETHOD(raw_GetCustomUI)(BSTR RibbonID, BSTR * RibbonXml);

private:
	HRESULT HrGetResource(int nId, 
		LPCTSTR lpType, 
		LPVOID* ppvResourceData,       
		DWORD* pdwSizeInBytes);

	BSTR GetXMLResource(int nId);

	SAFEARRAY* GetOFSResource(int nId);


private:
	bool Is_Outlook_2010_Version;
	CSyncSetting* spSyncSettingForm;
};

OBJECT_ENTRY_AUTO(__uuidof(AdditionalObj), CAdditionalObj)
